package com.xunyi.learndemo.queue;

import com.xunyi.learndemo.domain.OrderInfo;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

public class OrderDelayTask implements Delayed {
    private final OrderInfo order;
    private final long startTime;

    public OrderDelayTask(OrderInfo order, long delayTime) {
        this.order = order;
        this.startTime = System.currentTimeMillis() + delayTime;
    }

    public OrderInfo getOrder() {
        return order;
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return unit.convert(startTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
    }

    @Override
    public int compareTo(Delayed other) {
        return Long.compare(this.getDelay(TimeUnit.MILLISECONDS), other.getDelay(TimeUnit.MILLISECONDS));
    }

}
